package cn.chenmixuexi.config;

import cn.chenmixuexi.protocol.serializer.JDKSerializer;
import cn.chenmixuexi.protocol.serializer.JSONSerializer;
import cn.chenmixuexi.protocol.serializer.Serializer;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
@Configuration
@ComponentScan("cn.chenmixuexi")
@Slf4j
public class Config {
    public Config(){
    }
    static Properties properties;
    static {
        try (InputStream in = Config.class.getResourceAsStream("/application.properties")) {
            properties = new Properties();
            properties.load(in);
        } catch (IOException e) {
            throw new ExceptionInInitializerError(e);
        }
    }
    @Bean
    public int getServerPort() {
        String value = properties.getProperty("server.port");
        if(value == null) {
            return 8080;
        } else {
            return Integer.parseInt(value);
        }
    }
    @Bean
    public Serializer serializer() {
        JSONSerializer jsonSerializer = new JSONSerializer();
        log.debug("Use serializer "+jsonSerializer);
        return jsonSerializer;
    }



}
